<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- 表头 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addPartFn(data)">添加子部门</el-dropdown-item>
                    <el-dropdown-item @click.native="editFn(data)">编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形数据 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPartFn(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editFn(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPartFn(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <add-dept
        ref="AddDept"
        :show-view-dialog="showDialog"
        :cur-id="curId"
        :code-list="codeList"
        @colse-dialog="closeDialogFn"
        @update-depart="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentsAPI, delDepartmentAPI } from '@/api/departments'
import { transData } from '@/utils/index'
import AddDept from './components/add-dept'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curId: null, // 当前对象
      codeList: []// code数组
    }
  },
  mounted() {
    this.getDepartments()
  },
  methods: {
    // 封装的获取数据方法
    async getDepartments() {
      const res = await getDepartmentsAPI()
      console.log(res)
      this.codeList = res.data.depts.map(item => item.code)// code的数组
      this.list = transData(res.data.depts)// 将处理好的数据给list
    },
    // 删除部门
    delPartFn(id) {
      // element封装的函数
      this.$confirm('你确认要删除此部门吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除操作成功
        await delDepartmentAPI(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.getDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    // 关闭弹窗
    closeDialogFn() {
      this.showDialog = false
    },
    // 新增部门
    addPartFn(data) {
      // 显示弹框
      this.showDialog = true
      console.log(this.curId)
      this.curId = data// 这个id的值是一个对象
    },
    // 编辑部门
    editFn(data) {
      // 显示弹框
      this.showDialog = true
      // 记录当前点击项的数据
      this.curId = data
      this.$refs.AddDept.getDepartDetail(data.id)
    }
  }
}

</script>
